package com.paas.form.service.impl;

import com.paas.common.util.DateUtil;
import com.paas.common.web.service.BaseServiceImpl;
import com.paas.form.dao.FormPortalQueryDao;
import com.paas.form.entity.FormDataChangeInfoEntity;
import com.paas.form.enums.FormDataChangInfoEnums;
import com.paas.form.service.IFormPortalQueryService;
import com.paas.form.vo.FormMasterViewStatisticsVo;
import com.paas.tool.enums.DataChangInfoEnums;
import org.apache.commons.lang.time.DateUtils;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

@Service("formPortalQueryService")
public class FormPortalQueryServiceImpl extends BaseServiceImpl<FormPortalQueryDao, FormMasterViewStatisticsVo> implements IFormPortalQueryService {

    @Override
    public List<HashMap<String, Object>> queryDataChangeRecentData() {
        List<FormDataChangeInfoEntity> dataChangeInfos = this.getBaseMapper().queryDataChangeRecentData();
        List<HashMap<String, Object>> listResult = new ArrayList<>();
        HashMap<String, HashMap<String,Integer>> objMap = new HashMap<>();
        //封装初始化map
        SimpleDateFormat sdf=new SimpleDateFormat(DateUtil.DATE_FORMAT_YMD);
        for (int i = 0; i < 7; i++) {
            Date date = DateUtils.addDays(new Date(), -i);
            String formatDate = sdf.format(date);
            HashMap<String, Integer> map = new HashMap<>();
            DataChangInfoEnums.TypeEnum[] dataChangInfoEnums = DataChangInfoEnums.TypeEnum.values();
            for (int j = 0; j <dataChangInfoEnums.length ; j++) {
                DataChangInfoEnums.TypeEnum dataChangInfoEnum = dataChangInfoEnums[j];
                map.put(dataChangInfoEnum.getCode(),0);
            }
            objMap.put(formatDate,map);
        }
        //更新objMap初始化数据
        for (int i = 0; i <dataChangeInfos.size() ; i++) {
            FormDataChangeInfoEntity dataChangeInfo = dataChangeInfos.get(i);
            String date = sdf.format(dataChangeInfo.getCreateDate());
            HashMap<String, Integer> countMap = objMap.get(date);
            getCountMap(dataChangeInfo.getType(),countMap);
            objMap.put(date,countMap);
        }
        //将objMap处理好的数据装进listResult
        for (int i = 0; i < 7; i++) {
            Date date = DateUtils.addDays(new Date(), -i);
            String formatDate = sdf.format(date);
            HashMap<String, Object> resultMap = new HashMap<>();
            HashMap<String, Integer> countMap = objMap.get(formatDate);
            resultMap.put("daily",formatDate);
            DataChangInfoEnums.TypeEnum[] dataChangInfoEnums = DataChangInfoEnums.TypeEnum.values();
            for (int j = 0; j <dataChangInfoEnums.length ; j++) {
                DataChangInfoEnums.TypeEnum dataChangInfoEnum = dataChangInfoEnums[j];
                resultMap.put(dataChangInfoEnum.getCode(),countMap.get(dataChangInfoEnum.getCode()));
            }
            listResult.add(resultMap);
        }
        return listResult;
    }

    @Override
    public FormMasterViewStatisticsVo queryMasterDataOverview() {
        return this.getBaseMapper().queryMasterDataOverview();
    }

    @Override
    public FormMasterViewStatisticsVo queryMasterViewStatistics() {
        return this.getBaseMapper().queryMasterViewStatistics();
    }


    private void getCountMap(String type, HashMap<String, Integer> countMap){
        FormDataChangInfoEnums.TypeEnum[] dataChangInfoEnums = FormDataChangInfoEnums.TypeEnum.values();
        for (int i = 0; i <dataChangInfoEnums.length ; i++) {
            FormDataChangInfoEnums.TypeEnum dataChangInfoEnum = dataChangInfoEnums[i];
            if (type.equals(dataChangInfoEnum.getValue())){
                if (type.equals(DataChangInfoEnums.TypeEnum.deleteType.getValue())
                        || type.equals(DataChangInfoEnums.TypeEnum.enableType.getValue())
                        || type.equals(DataChangInfoEnums.TypeEnum.disableType.getValue())){
                    String code = DataChangInfoEnums.TypeEnum.enableType.getCode();
                    Integer count = countMap.get(code);
                    countMap.put(code,count+1);
                    break;
                }else {
                    Integer count = countMap.get(dataChangInfoEnum.getCode());
                    countMap.put(dataChangInfoEnum.getCode(),count+1);
                    break;
                }
            }
        }
    }
}
